﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure;
using Infrastructure.Extension;
using System.Threading;

namespace ZICConsoleHelp
{
    public class WiseLinkEncryptHelp
    {
        private ZICConsoleHelp.ZIS2Entities zis2Ent = new ZIS2Entities();

        public void Encrypt()
        {
            int count = 0;
            foreach (SrcTroubleCode[] srcChunk in zis2Ent.SrcTroubleCodes.OrderBy(c => c.TroubleCodeID).QueryChunksOfSize(5000))
            {
                var query = from src in srcChunk.AsParallel()
                            select src;
                query.ForAll(src =>
                    {

                        WiseLinkTroubleCode wtc = new WiseLinkTroubleCode();
                        wtc.TroubleCodeID = src.TroubleCodeID;
                        wtc.CarType = src.CarType;
                        wtc.CodeDescription = WiseLinkEncryptor.Encrypt(src.CodeDescription);
                        wtc.Cause = WiseLinkEncryptor.Encrypt(src.Cause);
                        wtc.Code = src.Code;
                        wtc.Severity = src.Severity;
                        wtc.Solution = WiseLinkEncryptor.Encrypt(src.Solution);
                        wtc.TroubleSymptom = WiseLinkEncryptor.Encrypt(src.TroubleSymptom);
                        wtc.UDScode = src.UDScode;
                        wtc.Version = src.Version;
                        lock (this)
                        {
                            zis2Ent.WiseLinkTroubleCodes.AddObject(wtc);
                            count++;
                        }
                        //lock (this)
                        //{
                        //    count++;
                        //}
                        
                    }
                );

                zis2Ent.SaveChanges();
                Console.WriteLine(count);
            }
        }

        public void Decrypt()
        {
            foreach (WiseLinkTroubleCode wtc in zis2Ent.WiseLinkTroubleCodes.Take(10))
            {
                string str = WiseLinkEncryptor.Decrypt(wtc.CodeDescription);
                Console.WriteLine(str);
            }
        }
    }
}
